package com.tr.rentcar.controller.loginandregister;


import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.dao.UserDao;

/**
 * Servlet implementation class Login
 * 登录验证信息
 */
@WebServlet("/login")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao udao = new UserDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("userinfo") != null){
			response.sendRedirect(request.getContextPath()+"/home");
			return;
		}
		request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String tel = request.getParameter("tel");
		String password = DigestUtils.md5Hex(request.getParameter("password"));
		String captcha = request.getParameter("yzmk");
		String sesionCode = (String) session.getAttribute(CaptchaController.CAPTCHA_KEY);
		try {
			User user=udao.findByTel(tel);
			if (StringUtils.isEmpty(captcha) || !captcha.equalsIgnoreCase(sesionCode)) {
				request.setAttribute("yerror", "验证码错误！");
				doGet(request, response);
				return;
			} else if (user != null && password != null) {
				if (password.equals(udao.findByTel(tel).getUpassword())) {
					session.setAttribute("userinfo", user);//将用户信息存放在session中
					session.setMaxInactiveInterval(3*60*60*60);
					sendLoginCookie(request, response, user);//发送cookie
					udao.insertLoginStatus(user.getUid());//插入一条登录信息
					//在这里开始判断是否有跳转页,有则跳转到指定页，没有则跳转主页
					if(session.getAttribute("goUrl")!=null){
						response.sendRedirect(session.getAttribute("goUrl")+"");
						session.removeAttribute("goUrl");
					}else{
						response.sendRedirect(request.getContextPath()+"/home");
					}
				} else {
					request.setAttribute("merror", "密码错误！");
					doGet(request, response);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送cookie到浏览器
	 * @param request
	 * @param response
	 * @param user
	 */
	private void sendLoginCookie(HttpServletRequest request,HttpServletResponse response,User user){
		Cookie cookie=new Cookie("autologin",user.getUtel()+"."+user.getUpassword());
		cookie.setMaxAge(3600);//设置有效期
		cookie.setPath(request.getContextPath());//设置有效路径
		response.addCookie(cookie);//将cookie写入浏览器
	}

}
